package com.ydcycloud.user.controller.common;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smart.util.StringUtils;
import com.smart.util.ValidateUtils;
import com.ydcycloud.base.bean.CommonResult;
import com.ydcycloud.base.enums.CommonEnums;
import com.ydcycloud.sys.controller.BaseWapController;
import com.ydcycloud.user.model.User;
import com.ydcycloud.user.service.UserService;

@Controller
@RequestMapping("/common/user")
public class UserCommonController extends BaseWapController {
	
	@Resource
	private UserService userService;

	/**
	 * 用户登录
	 * @param account
	 * @param password
	 * @return
	 */
	@RequestMapping(path = "/login", method = RequestMethod.POST)
	public @ResponseBody
	CommonResult login(String account, String password,HttpServletRequest request) {
		CommonResult r = new CommonResult();
		try {
			if (StringUtils.isBlank(account) || StringUtils.isBlank(password)) {
				commonEnums = CommonEnums.PARANETER_ERROR; // 302 参数错误
			} else {
				User user = userService.getLoginUser(account);
				if (user == null) {
					commonEnums = CommonEnums.ACCOUNT_OR_PASSWORD_ERROR;// 100
																		// 用户名或者密码错误
				} else {
					if (!user.getPassword().equals(password)) {
						commonEnums = CommonEnums.ACCOUNT_OR_PASSWORD_ERROR;// 100
																			// 用户名或者密码错误
					} else {
						Map<String, Object> map = new HashMap<String, Object>();
						map.put("name", user.getName());
						map.put("sex", user.getSex());
						map.put("idCard", user.getIdCard());
						map.put("phone", user.getPhone());
						map.put("area", user.getArea());
						map.put("education", user.getEducatin());
						map.put("wantJobId", user.getWantJobId());
						map.put("expTime",user.getExpTime());
						map.put("expDetail", user.getExpDetail());
						map.put("cardUrl", user.getCardUrl());
						map.put("headImageUrl", user.getHeadImageUrl());
						r.setData(map);
						user.setLastUpdateTime(new Date()); // 最后一次更新时间
						userService.save(user);
						
						request.getSession().setAttribute("userId", user.getId());
						commonEnums = CommonEnums.SUCCESS; // 300 成功
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			commonEnums = CommonEnums.SYSTEM_ERROR; // 301 系统错误
		} finally {
			r.setStatus(commonEnums.getValue());
			r.setMessage(commonEnums.getLabel());
		}
		return r;
	}
	
	/**
	 * 用户注册
	 * @param openid
	 * @param name
	 * @param sex
	 * @param idCard
	 * @param phone
	 * @param areaCode
	 * @param education
	 * @param headImageUrl
	 * @param request
	 * @return
	 */
	@RequestMapping("/register")
	public @ResponseBody CommonResult register(String name, 
			Integer sex,String idCard, String phone,String areaCode,Integer education,String headImageUrl,HttpServletRequest request) {
		CommonResult r = new CommonResult();
		try {
			if(StringUtils.isBlank(name) 
					|| sex == null || sex <1 || sex > 3 || StringUtils.isBlank(phone) 
					|| !ValidateUtils.isMobile(phone)){
				commonEnums = CommonEnums.PARANETER_ERROR;//302 参数错误
				return r;
			}
			User bean = new User();
			bean.setCreateTime(new Date());
			bean.setName(name);
			bean.setSex(sex);
			bean.setPassword("123456");
			bean.setPhone(phone);
			bean.setIdCard(idCard);
			bean.setArea(areaCode);
			bean.setEducatin(education);
			bean.setHeadImageUrl(headImageUrl);
			bean.setLastUpdateTime(new Date());

			Map<String, Object> map = (Map<String, Object>) request.getSession().getAttribute("weChatMap");
			if(map != null && map.size() > 0){
				bean.setName((String)map.get("name"));
				bean.setHeadImageUrl((String)map.get("headImageUrl"));
			}
			userService.save(bean);
			
			Map<String, Object> resultMap = new HashMap<String,Object>();
			resultMap.put("id", bean.getId());
			resultMap.put("name", bean.getName());
			resultMap.put("phone", bean.getPhone());
			resultMap.put("sex", bean.getSex());
			resultMap.put("idCard", bean.getIdCard());
			resultMap.put("area", bean.getArea());
			resultMap.put("education", bean.getEducatin());
			resultMap.put("wantJob", bean.getWantJobId());
			resultMap.put("expTime", bean.getExpTime());
			resultMap.put("expDetail", bean.getExpDetail());
			resultMap.put("cardUrl", bean.getCardUrl());
			resultMap.put("headImageUrl", bean.getHeadImageUrl());
			r.setData(resultMap);
			commonEnums = CommonEnums.SUCCESS;//300 成功
		}catch (Exception e) {
			e.printStackTrace();
			commonEnums = CommonEnums.SYSTEM_ERROR; // 301 系统错误
		} finally {
			r.setStatus(commonEnums.getValue());
			r.setMessage(commonEnums.getLabel());
		}
		return r;
	}
	
	
}
